package bar.dark.lee.setting;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import bar.dark.lee.BDConstents;
import bar.dark.lee.DBInit;
import bar.dark.lee.IntroPasswordActivity;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.setting.auto.AutoActivity;
import bar.dark.lee.setting.category.CategoryTabActivity;
import bar.dark.lee.setting.data.DataAutoOutActivity;
import bar.dark.lee.setting.data.DataInActivity;
import bar.dark.lee.setting.data.DataOutActivity;
import bar.dark.lee.setting.excel.ExcelActivity;
import bar.dark.lee.setting.fanokno.FanOkNoActivity;
import bar.dark.lee.setting.font.FontActivity;
import bar.dark.lee.setting.mybankauto.MyBankAutoActivity;
import bar.dark.lee.setting.password.PasswordActvitiy;
import bar.dark.lee.setting.postmoney.MonthPostMoney;
import bar.dark.lee.setting.sms.SMSActivity;
import bar.dark.lee.setting.sms.SmsNotiSettingActivity;
import bar.dark.lee.setting.statsdate.StartDateActivity;
import bar.dark.lee.setting.viewtype.MainViewTypeActivity;
import bar.dark.lee.setting.viewtype.WidgetViewTypeActivity;

public class SettingActivity extends SuperActivity implements OnClickListener
{
	private static final int REQUEST_PASSWORD = 0;
	
	public static String MONEY_FLAG = "money_flag";
	public static int REAL_MONEY = 0;
	public static int BANK_MONEY = 1;
	public static int CHECK_CARD = 2;
	public static int CREDIT_CARD = 3;
	
	private View categoryButton;
	private View initButton;
	private View autoButton;
	private View myBankButton;
	private View smsButton;
	private View smsNotiButton;
	private View startDateButton;
	private View mainViewTypeButton;
	private View widgetViewTypeButton;
	private View postButton;
	private View fontButton;
	private View tipsButton;
	private View developerButton;
	private View dataOutButton;
	private View dataInButton;
	private View passwordButton;
	private View excelButton;
	private View dataAutoButton;
	private View fanOkNoButton;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_activity);
		
		setTitle("설정");
		setGoneTipButton();
		
		categoryButton = (View) findViewById(R.id.settingCategoryButton);
		initButton = (View) findViewById(R.id.settingInitButton);
		autoButton = (View) findViewById(R.id.settingAutoButton);
		myBankButton = (View) findViewById(R.id.settingMyBankAutoButton);
		smsButton = (View) findViewById(R.id.settingSmsButton);
		smsNotiButton = (View) findViewById(R.id.settingSmsNotiButton);
		startDateButton = (View) findViewById(R.id.settingStartDate);
		mainViewTypeButton = (View) findViewById(R.id.settingMainViewType);
		widgetViewTypeButton = (View) findViewById(R.id.settingWidgetViewType);
		postButton = (View) findViewById(R.id.settingMonthPostMoney);
		fontButton = (View) findViewById(R.id.settingFont);
		tipsButton = (View) findViewById(R.id.settingTipsButton);
		developerButton = (View) findViewById(R.id.settingDeveloperButton);
		dataOutButton = (View) findViewById(R.id.settingDataOutButton);
		dataInButton = (View) findViewById(R.id.settingDataInButton);
		passwordButton = findViewById(R.id.settingPassword);
		excelButton = findViewById(R.id.settingExcel);
		dataAutoButton = findViewById(R.id.settingDataAutoOutButton);
		fanOkNoButton = findViewById(R.id.settingFanOkNo);
		
		categoryButton.setOnClickListener(this);
		initButton.setOnClickListener(this);
		autoButton.setOnClickListener(this);
		myBankButton.setOnClickListener(this);
		smsButton.setOnClickListener(this);
		smsNotiButton.setOnClickListener(this);
		startDateButton.setOnClickListener(this);
		mainViewTypeButton.setOnClickListener(this);
		widgetViewTypeButton.setOnClickListener(this);
		postButton.setOnClickListener(this);
		fontButton.setOnClickListener(this);
		tipsButton.setOnClickListener(this);
		developerButton.setOnClickListener(this);
		dataOutButton.setOnClickListener(this);
		dataInButton.setOnClickListener(this);
		passwordButton.setOnClickListener(this);
		excelButton.setOnClickListener(this);
		dataAutoButton.setOnClickListener(this);
		fanOkNoButton.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) 
	{
		if (v == categoryButton)
		{
			Intent intent = new Intent(this, CategoryTabActivity.class);
			startActivity(intent);
		}
		else if (v == initButton)
		{
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setIcon(android.R.drawable.ic_dialog_alert);
	    	dialog.setTitle("데이터 초기화");
	    	dialog.setMessage("데이터를 초기화 하시겠습니까?");
	    	dialog.setPositiveButton("초기화", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					setDataCancel();
					dialog.dismiss();
				}
			});
	    	dialog.setNegativeButton("취소", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					dialog.dismiss();
				}
			});
	    	
	    	dialog.show();
		}
		else if (v == autoButton)
		{
			startActivity(new Intent(this, AutoActivity.class));
		}
		else if (v == myBankButton)
		{
			startActivity(new Intent(this, MyBankAutoActivity.class));
		}
		else if (v == smsButton)
		{
			startActivity(new Intent(this, SMSActivity.class));
		}
		else if (v == smsNotiButton)
		{
			startActivity(new Intent(this, SmsNotiSettingActivity.class));
		}
		else if (v == startDateButton)
		{
			startActivity(new Intent(this, StartDateActivity.class));
		}
		else if (v == mainViewTypeButton)
		{
			startActivity(new Intent(this, MainViewTypeActivity.class));
		}
		else if (v == widgetViewTypeButton)
		{
			startActivity(new Intent(this, WidgetViewTypeActivity.class));
		}
		else if (v == fontButton)
		{
			startActivity(new Intent(this, FontActivity.class));
		}
		else if (v == developerButton)
		{
			startActivity(new Intent(this, DeveloperActivity.class));
		}
		else if (v == postButton)
		{
			startActivity(new Intent(this, MonthPostMoney.class));
		}
		else if (v == tipsButton)
		{
			startActivity(new Intent(this, TipsActivity.class));
		}
		else if (v == dataOutButton)
		{
			startActivity(new Intent(this, DataOutActivity.class));
		}
		else if (v == dataInButton)
		{
			startActivity(new Intent(this, DataInActivity.class));
		}
		else if (v == passwordButton)
		{
			startActivity(new Intent(this, PasswordActvitiy.class));
		}
		else if (v == excelButton)
		{
			startActivity(new Intent(this, ExcelActivity.class));
		}
		else if (v == dataAutoButton)
		{
			startActivity(new Intent(this, DataAutoOutActivity.class));
		}
		else if (v == fanOkNoButton)
		{
			startActivity(new Intent(this, FanOkNoActivity.class));
		}
	}
	
	private void setDataCancel()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int passwordSetting = pref.getInt(BDConstents.PASSWORD_SETTING, 0);
		
		if (passwordSetting == 1)
		{
			Intent intent = new Intent(this, IntroPasswordActivity.class);
			intent.putExtra(IntroPasswordActivity.MODE, 1);
			startActivityForResult(intent, REQUEST_PASSWORD);
		}
		else
		{
			new DBInitBackground().execute(null);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == REQUEST_PASSWORD)
		{
			if (resultCode == Activity.RESULT_OK)
			{
				new DBInitBackground().execute(null);
			}
		}
	}
	
	/**
	 * 디비 초기화 
	 * @author hyojun
	 */
	private class DBInitBackground extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(SettingActivity.this);
			dialog.setCancelable(false);
			dialog.setCancelable(false);
			dialog.setMessage("초기화 중입니다...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) 
		{
			DBInit init = new DBInit(SettingActivity.this);
			init.initDropTable(SettingActivity.this);
			init.setTable(SettingActivity.this);
			SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
	    	SharedPreferences.Editor editor = pref.edit();
	    	editor.clear();
	    	editor.commit();
			finish();
			return null;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			dialog.dismiss();
		}
	}

	@Override
	protected void tipButton()
	{
	}
}
